package com.guard.aws;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 8/27/21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：MQTT状态监听
 */
public class AwsMQTTConnectObserver {

    private static volatile AwsMQTTConnectObserver mInstance = null;

    private final List<AwsMQTTCallBackObserver> mObservers = new ArrayList<>();

    public static AwsMQTTConnectObserver getInstance() {
        if (mInstance == null){
            synchronized (AwsMQTTConnectObserver.class) {
                if (mInstance == null) {
                    mInstance = new AwsMQTTConnectObserver();
                }
            }
        }
        return mInstance;
    }

    public void registerObserver(AwsMQTTCallBackObserver observer){
        if (observer == null) return;
        if (!mObservers.contains(observer)){
            mObservers.add(observer);
        }
    }

    public void unRegisterObserver(AwsMQTTCallBackObserver observer){
        if (observer == null) return;
        mObservers.remove(observer);
    }

    public void onMQTTReceive(String str) {
        for (AwsMQTTCallBackObserver callback:mObservers) {
            callback.onMQTTReceive(str);
        }
    }

    public void onMQTTDisconnect() {
        for (AwsMQTTCallBackObserver callback:mObservers) {
            callback.onMQTTDisconnected();
        }
    }
}
